package cn.wh.ymcc.serivce.impl;

import cn.wh.ymcc.doc.CourseDoc;
import cn.wh.ymcc.dto.CourseSearchDto;
import cn.wh.ymcc.esmapper.HighlightResultMapper;
import cn.wh.ymcc.repository.CourseEsRepository;
import cn.wh.ymcc.result.PageList;
import cn.wh.ymcc.serivce.ICourseEsService;
import cn.wh.ymcc.vo.AggrsBucket;
import cn.wh.ymcc.vo.EsAggrPageList;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.*;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseEsServiceImpl implements ICourseEsService {

    @Autowired
    private CourseEsRepository courseEsRepository;//es的crud

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//es的高亮
    @Autowired
    private HighlightResultMapper highlightResultMapper;

    @Override
    public void saveCourse(CourseDoc doc) {
        courseEsRepository.save(doc);
    }

    @Override
    public void deleteCourse(Long courseId) {
        courseEsRepository.deleteById(courseId);
    }

    @Override
    public PageList<CourseDoc> search(CourseSearchDto dto) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();

        //封装排序条件========================
        if(StringUtils.hasLength(dto.getSortField())){
            // dto.getSortField()  xl rq  xp pl
            String sortField = null;
            switch (dto.getSortField().toLowerCase()){
                case "xl": sortField="saleCount";break;
                case "xp": sortField="onlineTime";break;
                case "pl": sortField="commentCount";break;
                case "jg": sortField="price";break;
                case "rq": sortField="viewCount";break;
                default: break;
            }
            if(sortField != null){
                if(StringUtils.hasLength(dto.getSortType())){
                    SortOrder sortOrder = dto.getSortType().equalsIgnoreCase("ASC") ? SortOrder.ASC : SortOrder.DESC;
                    builder.withSort(new FieldSortBuilder(sortField).order(sortOrder));
                }
            }
        }
        //封装分页相关的参数===================  0 代表第一页
        builder.withPageable(PageRequest.of(dto.getPage()-1, dto.getRows()));
        //PageRequest pageRequest = PageRequest.of(dto.getPage()-1, dto.getRows());
        //封装查询条件========================
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //DSL查询  private String keyword;//关键字-------
        if(StringUtils.hasLength(dto.getKeyword())){
            boolQuery.must(QueryBuilders.matchQuery("name",dto.getKeyword()));
        }
        //DSL过滤-----------------
        if(dto.getCourseTypeId() != null){
            boolQuery.filter(QueryBuilders.termQuery("courseTypeId",dto.getCourseTypeId()));
        }
        if(StringUtils.hasLength(dto.getGradeName())){
            boolQuery.filter(QueryBuilders.termQuery("gradeName",dto.getGradeName()));
        }
        if(StringUtils.hasLength(dto.getChargeName())){
            boolQuery.filter(QueryBuilders.termQuery("chargeName",dto.getChargeName()));
        }
        if(dto.getPriceMin() != null){
            boolQuery.filter(QueryBuilders.rangeQuery("price").gte(dto.getPriceMin()));
        }
        if(dto.getPriceMax() != null){
            boolQuery.filter(QueryBuilders.rangeQuery("price").lte(dto.getPriceMax()));
        }
        //设置高亮 1.样式设置 2.处理映射器结果
        builder.withHighlightFields(
                new HighlightBuilder.Field("name")
                        .preTags("<span style='color:red'>")
                        .postTags("</span>")
        );

        //添加聚合查询条件=================================
        //1.设置搜索条件   2.处理搜索结果成前端需要的数据结构
        //添加等级名字聚合
        builder.addAggregation(AggregationBuilders.terms("aggsGradeName").field("gradeName"));
        //添加收费规则聚合
        builder.addAggregation(AggregationBuilders.terms("aggsChargeName").field("chargeName"));

        //搜索Es  ==========================
        builder.withQuery(boolQuery);
        NativeSearchQuery build = builder.build();
        //Page<CourseDoc> page = courseEsRepository.search(build);//  ##########
        AggregatedPage<CourseDoc> page = elasticsearchRestTemplate.queryForPage(build,CourseDoc.class,highlightResultMapper);
        //处理聚合结果
        //拿到所有的聚合结果
        //1.拿到所有的聚合结果,整成map结构
        Aggregations aggregations = page.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        ParsedStringTerms aggsGradeName = (ParsedStringTerms) aggregationMap.get("aggsGradeName");
        ParsedStringTerms aggsChargeName = (ParsedStringTerms)aggregationMap.get("aggsChargeName");

        //处理等级聚合结果
        List<AggrsBucket> aggrsBuckets = new ArrayList<>();
        List<? extends Terms.Bucket> buckets = aggsGradeName.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            String key = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
            AggrsBucket aggrsBucket = new AggrsBucket(key, docCount);
            aggrsBuckets.add(aggrsBucket);
        }

        List<AggrsBucket> aggrsBuckets1 = new ArrayList<>();
        List<? extends Terms.Bucket> buckets1 = aggsChargeName.getBuckets();
        for (Terms.Bucket bucket : buckets1) {
            String key = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
            AggrsBucket aggrsBucket = new AggrsBucket(key, docCount);
            aggrsBuckets1.add(aggrsBucket);
        }

        Map<String, List<AggrsBucket>> map = new HashMap<>();
        map.put("aggsGradeName",aggrsBuckets);
        map.put("aggsChargeName",aggrsBuckets1);

        //封装PageList返回 ==================
        return new EsAggrPageList<>(page.getTotalElements(),page.getContent(),map);
    }


}



